package com.example.myapplication6.bean;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class tuangouActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private static final String TAG = "tuangouActivity";
    private ListView listView;
    private List<meituan> meituans;
    private Button meituan;
    private Button lashou;
    private Button nuomi;
    private Button ftuan;
    private Button city_bt;
    private TextView city_text;
    private ProgressDialog Dialog;
    private SimpleAdapter adapter;
    private String[] citys_china;
    private String[] citys;
    private int city_position;
    private String city_str;

    public tuangouActivity() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(1);
        this.setContentView(2130903045);
        this.citys = this.getResources().getStringArray(2131099648);
        this.citys_china = this.getResources().getStringArray(2131099649);
        this.ReadSharedPreferences();
        this.city_str = this.citys_china[this.city_position];
        this.city_text = (TextView)this.findViewById(2131230746);
        this.Dialog = new ProgressDialog(this);
        this.Dialog.setTitle("");
        this.Dialog.setMessage("正在载入,请稍候...");
        String path = "http://open.client.lashou.com/api/detail/city/" + URLEncoder.encode(this.citys_china[this.city_position]) + "/p/1/r/10";
        this.progressshow(path, 1);
        this.meituan = (Button)this.findViewById(2131230748);
        this.meituan.setOnClickListener(this);
        this.lashou = (Button)this.findViewById(2131230749);
        this.lashou.setOnClickListener(this);
        this.ftuan = (Button)this.findViewById(2131230750);
        this.ftuan.setOnClickListener(this);
        this.nuomi = (Button)this.findViewById(2131230751);
        this.nuomi.setOnClickListener(this);
        this.city_bt = (Button)this.findViewById(2131230747);
        this.city_bt.setOnClickListener(this);
        this.listView = (ListView)this.findViewById(2131230729);
        this.listView.setOnItemClickListener(this);
    }

    public void onClick(View v) {
        String path;
        if (v == this.meituan) {
            path = "http://www.meituan.com/api/v2/" + this.citys[this.city_position] + "/deals";
            this.progressshow(path, 0);
        } else if (v == this.lashou) {
            path = "http://open.client.lashou.com/api/detail/city/" + URLEncoder.encode(this.citys_china[this.city_position]) + "/p/1/r/10";
            this.progressshow(path, 1);
        } else if (v == this.ftuan) {
            path = "http://newapi.ftuan.com/api/v2.aspx?city=" + this.citys[this.city_position];
            this.progressshow(path, 2);
        } else if (v == this.nuomi) {
            path = "http://www.nuomi.com/api/dailydeal?version=v1&city=" + this.citys[this.city_position];
            this.progressshow(path, 3);
        } else if (v == this.city_bt) {
            AlertDialog.Builder city_dialog = new AlertDialog.Builder(this);
            city_dialog.setTitle("请选择您需要查看的商品");
            city_dialog.setSingleChoiceItems(this.citys_china, -1, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    tuangouActivity.this.city_position = which;
                    tuangouActivity.this.city_str = tuangouActivity.this.citys_china[tuangouActivity.this.city_position];
                    Toast.makeText(tuangouActivity.this, "您现在选择查看的商品为" + tuangouActivity.this.citys_china[tuangouActivity.this.city_position], 1).show();
                    String path = "http://open.client.lashou.com/api/detail/city/" + URLEncoder.encode(tuangouActivity.this.citys_china[tuangouActivity.this.city_position]) + "/p/1/r/10";
                    tuangouActivity.this.progressshow(path, 1);
                    dialog.dismiss();
                }
            });
            city_dialog.show();
        }

    }

    private void progressshow(final String path, final int web) {
        this.Dialog.show();
        final Handler mHandler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    tuangouActivity.this.Dialog.cancel();
                    tuangouActivity.this.city_text.setText(tuangouActivity.this.city_str);
                    tuangouActivity.this.listView.setAdapter(tuangouActivity.this.adapter);
                }

            }
        };
        (new Thread() {
            public void run() {
                Looper.prepare();

                try {
                    Thread.sleep(500L);
                } catch (InterruptedException var2) {
                    var2.printStackTrace();
                }

                tuangouActivity.this.urlconn(path, web);
                Message m = new Message();
                m.what = 1;
                mHandler.sendMessage(m);
                Looper.loop();
            }
        }).start();
    }

    private void urlconn(String path, int website) {
        try {
            URL url = new URL(path);
            HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
            int responseCode = httpConn.getResponseCode();
            if (responseCode == 200) {
                InputStream input = httpConn.getInputStream();
                this.meituans = tuan_handler.getListMeituan(input, website);
                this.setInListView();
                input.close();
                httpConn.disconnect();
            }
        } catch (Exception var7) {
            Log.e("tuangouActivity", var7.toString());
            Toast.makeText(this, "您的网络连接出错，请确认你的网络已打开", 1).show();
        }

    }

    private void setInListView() {
        List<Map<String, String>> lists = new ArrayList();
        Iterator var3 = this.meituans.iterator();

        while(var3.hasNext()) {
            meituan mei = (meituan)var3.next();
            Map<String, String> map = new HashMap();
            map.put("Website", "【" + mei.getWebsite() + "】");
            map.put("Deal_title", mei.getDeal_title());
            map.put("Price", "现价 : ¥ " + mei.getPrice());
            map.put("Value", "原价 : ¥ " + mei.getValue());
            map.put("Rebate", "折扣 : " + mei.getRebate());
            lists.add(map);
        }

        this.adapter = new SimpleAdapter(this, lists, 2130903041, new String[]{"Website", "Deal_title", "Price", "Value", "Rebate"}, new int[]{2131230724, 2131230725, 2131230726, 2131230727, 2131230728});
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = this.getMenuInflater();
        inflater.inflate(2131165184, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 2131230730:
                this.finish();
                break;
            case 2131230752:
                Intent intent = new Intent();
                intent.setAction("com.kang.mycollectActivity");
                this.startActivity(intent);
                break;
            case 2131230753:
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle(2131034127);
                final LinearLayout author = (LinearLayout)this.getLayoutInflater().inflate(2130903040, (ViewGroup)null);
                dialog.setView(author);
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        EditText edit_suggest = (EditText)author.findViewById(2131230723);
                        String suggest = edit_suggest.getText().toString();

                        try {
                            Intent emailIntent = new Intent("android.intent.action.SEND");
                            emailIntent.setType("plain/text");
                            emailIntent.putExtra("android.intent.extra.EMAIL", "liuzihan@126.com");
                            emailIntent.putExtra("android.intent.extra.SUBJECT", "处理订单意见");
                            emailIntent.putExtra("android.intent.extra.TEXT", suggest);
                            tuangouActivity.this.startActivity(Intent.createChooser(emailIntent, "订单发送中...."));
                        } catch (Exception var6) {
                            Log.e("tuangouActivity", var6.toString());
                            Toast.makeText(tuangouActivity.this, "请设置您手机里的收货地址", 1).show();
                        }

                    }
                });
                dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case 2131230754:
                this.finish();
        }

        return true;
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        meituan mei = (meituan)this.meituans.get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("mei", mei);
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setAction("com.kang.showdescActivity");
        this.startActivity(intent);
    }

    private void ReadSharedPreferences() {
        SharedPreferences city_info = this.getSharedPreferences("city_info", 0);
        this.city_position = city_info.getInt("CITY_POSITION", 0);
    }

    private void WriteSharedPreferences(int city_position) {
        SharedPreferences city_info = this.getSharedPreferences("city_info", 0);
        SharedPreferences.Editor edit = city_info.edit();
        edit.putInt("CITY_POSITION", city_position);
        edit.commit();
    }

    protected void onStop() {
        super.onStop();
        this.WriteSharedPreferences(this.city_position);
    }

    protected void onDestroy() {
        super.onDestroy();
        this.WriteSharedPreferences(this.city_position);
    }
}
